package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;;
import com.itheima.entity.QueryPageBeano;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Order;
import com.itheima.service.CheckGroupService;
import com.itheima.service.OrderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    //新增预约
    @RequestMapping("/submit")
    public Result submit(@RequestBody Order order, Integer setmealId){
        Result result;
        try {
            result = orderService.submit(order, setmealId);
        }catch (Exception e){
            //新增失败
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        //新增成功
        return result;
    }




    //分页查询
    @RequestMapping("/findOrdersPage")
    public PageResult findOrdersPage(@RequestBody QueryPageBeano queryPageBean){
        PageResult pageResult = orderService.findOrdersPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findAllById(id);
            return new Result(true, "预约设置信息", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取预约设置信息失败");
        }
    }

    //根据预约id查询套餐id啊
    @RequestMapping("/findSetmealIdByOrderId")
    public Result findSetmealIdByOrderId(int id) {
        int setmealId = orderService.findSetmealIdByOrderId(id);
        return new Result(true, "成功", setmealId);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order, Integer setmealId){
        try {
            orderService.edit(order, setmealId);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除预约信息失败");
        }
        return new Result(true, "删除预约信息成功");
    }

    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Order order){
        try{
            orderService.changeStatus(order);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS);
    }
}
